package com.lngsyz.eseal.base.biz.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lngsyz.eseal.base.entity.LoginInfo;
import com.lngsyz.eseal.base.entity.biz.InUserInfo;
import com.lngsyz.eseal.base.entity.source1.TUser;
import com.lngsyz.eseal.base.exception.RequestSystemException;
import com.lngsyz.eseal.base.mapper.source1.TUserMapper;

@Service
public class TUserService {
    
	@Autowired
	TUserMapper tUserMapper;

	/**
	 * 校验手机号是否已注册
	 * @param mobile
	 * @return  true:已注册  false：未注册
	 * @throws Exception
	 */
	public boolean checkExist(String mobile) throws Exception {
		if(StringUtils.isNotBlank(mobile)) {
			Map<String, Object> uParam = new HashMap<String, Object>();
			uParam.put("mobile", mobile);
			int usersCount = tUserMapper.findByConditionCount(uParam);
			if(usersCount > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			throw new RequestSystemException("账号为空");
		}
    }
	
	public TUser insert(TUser tUser) throws Exception {
		String mobile = tUser.getMobile();
		if(!checkExist(mobile)) {
			// 未注册
			// 检查是否是数字构成
			boolean isNumeric = mobile.matches("\\d+"); // 使用正则表达式进行校验
			if (isNumeric) {
				if(StringUtils.length(mobile) == 11) {
					tUser.setAdmin("0");
					tUserMapper.insert(tUser);
					return tUser;
				} else {
					throw new RequestSystemException("账号由11位手机号码构成");
				}
			} else {
				throw new RequestSystemException("账号存在非数字");
			}
		} else {
			// 已注册
			throw new RequestSystemException("账号已存在");
		}
	}

	/**
	 * 查询用户列表
	 * @param mobile
	 * @param district（4位）
	 * @param admin（空：不过滤 0：普通账号 1：管理员）
	 * @return
	 * @throws Exception
	 */
	public List<TUser> findList(InUserInfo inUserInfo, int page, int size) throws Exception {
		Map<String, Object> uParam = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(inUserInfo.getAccount())) {
			uParam.put("mobile", inUserInfo.getAccount());
		}
		if(StringUtils.isNotBlank(inUserInfo.getDistrict())) {
			uParam.put("districtCity", inUserInfo.getDistrict());
		}
		if(StringUtils.isNotBlank(inUserInfo.getAdmin())) {
			uParam.put("admin", inUserInfo.getAdmin());
		}
		uParam.put("minSize", page <= 0 ? 0 : (page-1) * size);
		uParam.put("maxSize", page <= 0 ? size : page * size);
		uParam.put("order", "GMT_CREATE DESC,ID");
		List<TUser> users = tUserMapper.findByCondition(uParam);
		return users;
	}
	
	/**
	 * 查询记录数量
	 * @param inUserInfo
	 * @return
	 * @throws Exception
	 */
	public int findListCount(InUserInfo inUserInfo) throws Exception {
		Map<String, Object> uParam = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(inUserInfo.getAccount())) {
			uParam.put("mobile", inUserInfo.getAccount());
		}
		if(StringUtils.isNotBlank(inUserInfo.getDistrict())) {
			uParam.put("districtCity", inUserInfo.getDistrict());
		}
		if(StringUtils.isNotBlank(inUserInfo.getAdmin())) {
			uParam.put("admin", inUserInfo.getAdmin());
		}
		int count = tUserMapper.findByConditionCount(uParam);
		return count;
	}

	/**
	 * 根据手机号查询一个用户
	 * @param mobile
	 * @return
	 * @throws Exception
	 */
	public TUser findOneByMobile(String mobile) throws Exception {
		Map<String, Object> uParam = new HashMap<String, Object>();
		uParam.put("mobile", mobile);
		uParam.put("minSize", 0);
		uParam.put("maxSize", 1);
		List<TUser> users = tUserMapper.findByCondition(uParam);
		if(users != null && users.size() > 0) {
			return users.get(0);
		} else {
			return null;
		}
	}
	
	/**
	 * 根据ID查询一个用户
	 * @param mobile
	 * @return
	 * @throws Exception
	 */
	public TUser findOneById(String id) throws Exception {
		TUser users = tUserMapper.findById(id);
		if(users != null) {
			return users;
		} else {
			return null;
		}
	}

	/**
	 * 校验密码正缺性
	 * @param mobile
	 * @param passwd
	 * @return true:一致  false：不正确
	 * @throws Exception
	 */
	public boolean checkPasswd(String mobile, String passwd) throws Exception {
		Map<String, Object> uParam = new HashMap<String, Object>();
		uParam.put("mobile", mobile);
		uParam.put("minSize", 0);
		uParam.put("maxSize", 1);
		List<TUser> users = tUserMapper.findByCondition(uParam);
		if(users != null && users.size() > 0) {
			TUser tUser = users.get(0);
			if(StringUtils.equals(tUser.getPasswd(), passwd)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	public void updatePasswd(String mobile, String newPasswd) {
		Map<String, Object> uParam = new HashMap<String, Object>();
		uParam.put("mobile", mobile);
		uParam.put("minSize", 0);
		uParam.put("maxSize", 1);
		List<TUser> users = tUserMapper.findByCondition(uParam);
		if(users != null && users.size() > 0) {
			TUser tUser = users.get(0);
			tUser.setPasswd(newPasswd);
			tUserMapper.updatePasswd(tUser);
		} else {
			throw new RequestSystemException("未找到当前账号");
		}
	}
	
	public void logout(LoginInfo loginInfo) {
		
	}
}
